using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using System.Reflection;
using System.Xml;

namespace UltimateDefend
{
    public partial class LoadFrm : Form
    {
        static public int nProgress = 0;
        static public bool m_bHasLoaded = false;
        static public string startup;

        public LoadFrm()
        {
            InitializeComponent();
        }

        private void LoadFrm_Load(object sender, EventArgs e)
        {
            progLoading.Width = ClientRectangle.Width - 20;
            progLoading.Left = 20 / 2;

            progLoading.Top = ClientRectangle.Bottom - 15 - progLoading.Height;

            startup = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
         
            Invalidate();
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            timer.Enabled = false;

            if (!m_bHasLoaded)
            {
                Thread tLoadCreeps = new Thread(new ThreadStart(LoadCreeps));

                tLoadCreeps.Start();

                while (nProgress < 100)
                    progLoading.Value = nProgress;
                
                // Load xong: 100%
                m_bHasLoaded = true;
                progLoading.Value = nProgress;
                MainFrm mainFrm = new MainFrm();
                mainFrm.ShowDialog();


            }
        }

        private void LoadCreeps()
        {
            XmlTextReader creepData = new XmlTextReader(startup + "\\Engine\\creep.xml");

            FootMan.LoadData(creepData);
            HeavyArmor.LoadData(creepData);
            IBoss.LoadData(creepData);

            creepData.Close();
            nProgress = 100;
        }


        private void SetProgress()
        {
            progLoading.Invoke(new SetProgressBarCallback(SetProgressBar));
        }
        delegate void SetProgressBarCallback();
        private void SetProgressBar()
        {
            progLoading.Value = nProgress;
        }

        private void LoadFrm_Paint(object sender, PaintEventArgs e)
        {
            Bitmap bgnd = Properties.Resources.Loading;
            e.Graphics.DrawImage(bgnd, ClientRectangle, new Rectangle(0, 0, bgnd.Width, bgnd.Height), GraphicsUnit.Pixel);
            bgnd.Dispose();
        }

        private void LoadFrm_GotFocus(object sender, EventArgs e)
        {
            if (m_bHasLoaded)
                Application.Exit();
        }
    }
}